/*
    XTU OJ: 1387
    by: fifth_light
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>

int array(int64_t arr[], int a, int b) {
    arr[0] = 1;
    arr[1] = 1;
    int i;
    for (i = 2;; i++) {
        int64_t num = a * arr[i - 1] + b * arr[i - 2];
        if (num > 1000000000 || num <= 0) {
            break;
        }
        arr[i] = num;
    }
    return i;
}

int cmp(const void* p1, const void* p2) {
    int64_t v1 = *((int64_t*) p1);
    int64_t v2 = *((int64_t*) p2);
    if (v1 == v2) {
        return 0;
    } else {
        return v1 > v2 ? 1 : -1;
    }
}

int calc(int a, int b, int c, int d, int l, int r) {
    if (r < l) {
        return 0;
    }

    int64_t x[128], y[128];
    int x_len = array(x, a, b), y_len = array(y, c, d);
    int64_t z[4800];
    int all_len = 0;
    for (int ix = 0; ix < x_len; ix++) {
        for (int iy = 0; iy < y_len; iy++) {
            int number = x[ix] + y[iy];
            if (number >= l && number <= r) {
                z[all_len++] = number;
            }
        }
    }
    
    qsort(z, all_len, sizeof(int64_t), cmp);
    
    int max_length = 0, length = 0;
    int64_t last_number = -1;
    for (int i = 0; i < all_len; i++) {
        if (i == 0 || z[i - 1] != z[i]) {
            // 读入新数字
            int64_t number = z[i];
            if (number >= l && number <= r) {
                if (length == 0) {
                    length = 1;
                } else {
                    if (last_number + 1 != number) {
                        if (length > max_length) {
                            max_length = length;
                        }
                        length = 1;
                    } else {
                        length++;
                    }
                }
                last_number = number;
            } else if (number > r) {
                break;
            }
        }
    }
    if (length > max_length) {
        max_length = length;
    }
    return max_length;
}

int main(void) {
    int t;
    scanf("%d", &t);
    for(int i = 0; i < t; i++) {
        int a, b, c, d, l, r;
        scanf("%d%d%d%d%d%d", &a, &b, &c, &d, &l, &r);
        printf("%d\n", calc(a, b, c, d, l, r));
    }
    return 0;
}
